
#include "util.h"
#include "rutil/Time.hxx"

using namespace codegen;
using namespace resip;

LogSystem LogSystem::LOGMODULE("CodeGen");

ElapsedTime::ElapsedTime() : last_(resip::ResipClock::getTimeMicroSec())
{
}

void ElapsedTime::reset()
{
    last_ = resip::ResipClock::getTimeMicroSec();
}

resip::Data ElapsedTime::elapsed_and_reset()
{
    resip::Data duration(elapsed());
    reset();
    return duration;
}

resip::Data codegen::ElapsedTime::elapsed() const
{
    resip::Data duration;
    {
        DataStream ds(duration);
        UInt64 dur = resip::ResipClock::getTimeMicroSec() - last_;
        ds << (uint64_t)(dur / 1e6) << "s"
            << (uint64_t)((uint64_t)(dur % (uint64_t)1e6) / 1e3)
            << "ms" << dur % (uint64_t)1e3 << "us";
    }
    return duration;
}
